import { createMachine } from "xstate";

export const player_machine = createMachine({
  context: {},
  id: "Untitled",
  initial: "player_stopped",
  states: {
    player_stopped: {
      on: {
        start_play: {
          target: "player_starting",
        },
        "*": {
          target: "player_stopped",
          actions: {
            type: "unsupport_event",
          },
        },
      },
      entry: {
        type: "player_stopped_entry",
      },
    },
    player_starting: {
      invoke: {
        id: "start_player",
        input: ({context, event: {param}}) => ({param}),
        onDone: {
          target: "player_started",
          actions: {
            type: "start_player_done",
          },
        },
        onError: {
          target: "player_stopped",
          actions: {
            type: "start_player_error",
          },
        },
        src: "start_player",
      },
    },
    player_started: {
      on: {
        stop_play: {
          target: "player_stopping",
        },
        pause_play: {
          target: "player_pausing",
        },
        feed_data: {
          target: "player_started",
          actions: {
            type: "on_feed_data",
          },
        },
        "*": {
          target: "player_started",
          actions: {
            type: "unsupport_event",
          },
        },
      },
      entry: {
        type: "player_started_entry",
      },
    },
    player_stopping: {
      invoke: {
        id: "stop_player",
        input: ({context, event: {param}}) => ({param}),
        onDone: {
          target: "player_stopped",
          actions: {
            type: "stop_player_done",
          },
        },
        onError: {
          target: "player_stopped",
          actions: {
            type: "stop_player_error",
          },
        },
        src: "stop_player",
      },
    },
    player_pausing: {
      invoke: {
        id: "pause_player",
        input: ({context, event: {param}}) => ({param}),
        onDone: {
          target: "player_paused",
          actions: {
            type: "pause_player_done",
          },
        },
        onError: {
          target: "player_started",
          actions: {
            type: "pause_player_error",
          },
        },
        src: "pause_player",
      },
    },
    player_paused: {
      on: {
        resume_play: {
          target: "player_resuming",
        },
        stop_play: {
          target: "player_stopping",
        },
        "*": {
          target: "player_paused",
          actions: {
            type: "unsupport_event",
          },
        },
      },
      entry: {
        type: "player_paused_entry",
      },
    },
    player_resuming: {
      invoke: {
        id: "resume_player",
        input: ({context, event: {param}}) => ({param}),
        onDone: {
          target: "player_started",
          actions: {
            type: "resume_player_done",
          },
        },
        onError: {
          target: "player_paused",
          actions: {
            type: "resume_player_error",
          },
        },
        src: "resume_player",
      },
    },
  },
});